using Digitalmes.Application.Scada.Commands;
using ThingsEdge.Exchange.Contracts;
using ThingsEdge.Exchange.Forwarders;

namespace Digitalmes.Infrastructure.Scada.Handlers;

/// <summary>
/// 设备警报处理。
/// </summary>
internal sealed class EquipmentAlarmHandler(ISender sender) : IScadaTellHandler
{
    public async Task HandleAsync(NoticeContext context, CancellationToken cancellationToken = default)
    {
        var message = context.Message;

        var alarmData = message.Self().GetBitArray();
        var lastAlarmData = context.LastMasterPayloadData?.GetBitArray() ?? [];

        EquipmentAlarmCommand command = new()
        {
            LineCode = message.Schema.ChannelName,
            DeviceName = message.Schema.DeviceName,
            WsCode = message.Schema.TagGroupName,
            AlarmData = alarmData,
            LastAlarmData = lastAlarmData
        };

        await sender.Send(command, cancellationToken);
    }
}
